package com.link.gateway.core.filter.local;

import com.link.gateway.core.comproperty.FilterOrderConstants;
import com.link.gateway.core.comproperty.OtherConstants;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 局部过滤器，给exchange添加属性，在全局过滤器中根据此属性决定是拦截使用此过滤器的路由
 * @author 郭闪
 * @date 2019/12/11
 */
@Component
public class IgnoreGlobalFilter  extends AbstractGatewayFilterFactory {




    @Override
    public GatewayFilter apply(Object config) {
        return new InnerFilter();
    }

    /**
     * 创建一个内部类，来实现2个接口，指定顺序
     */
    private class InnerFilter implements GatewayFilter, Ordered {

        /**
         * 向exchange 添加ATTRIBUTE_IGNORE_GLOBAL_FILTER 属性
         * @author 郭闪
         * @date 2019/12/11
         * @param exchange
         * @param chain
         */
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            exchange.getAttributes().put(OtherConstants.ATTRIBUTE_IGNORE_GLOBAL_FILTER, true);
            return chain.filter(exchange);
        }

        @Override
        public int getOrder() {
            return FilterOrderConstants.IGNORE_GLOBAL_FILTER_ORDER;
        }
    }


}
